package br.cnpq.webflowah.ontologia;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLKnowledgeBase;

public class Repositorio {

	public static final String REASONER_API = "Pellet";
	
	public static final String CACHE_DIR = "./kb/";
	
	private static final String IDX_FILE = CACHE_DIR + "service.idx";
	
	private static OWLKnowledgeBase kb = null;
	
	// cria o arquivo caso nao exista 
	static {
		File file = new File(IDX_FILE);
		if (!file.exists()) {
			try {
				FileOutputStream fos = new FileOutputStream(file);
				fos.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}		
		}	
	}			
	
	public static OWLKnowledgeBase getInstance() {
		if (kb == null) {
			kb = OWLFactory.createKB();
			kb.setReasoner(REASONER_API);
			kb.getReader().getCache().setLocalCacheDirectory(CACHE_DIR);
			kb.getReader().getCache().setForced(true);			
		}
		
		kb.getReader().clear();

		return kb;
	}
	
	public static OWLKnowledgeBase newInstance(boolean cache) {
		OWLKnowledgeBase kb = OWLFactory.createKB();
		kb.setReasoner(REASONER_API);
		if (cache) {
			kb.getReader().getCache().setLocalCacheDirectory(CACHE_DIR);
			kb.getReader().getCache().setForced(true);
		}
		return kb;
	}
	
}
